/***
 * 系统插件
 */
$.extend({
	//系统请求
	request: function(_url, _data, _fn, _load) {
		var _opt = {};
		if (arguments.length == 1) {
			if (!$.isEmptyObject(_url)) {
				_opt = _url;
			} else {
				$.tip("请求参数不合法");
				return false;
			}
		} else if (arguments.length == 2) {
			_opt.url = _url;
			if ( $.isFunction(_data) ) {
				_opt.success = _data;
			} else {
				_opt.data = _data;
			}
		} else {
			_opt = {
				url: _url,
				data: _data,
				success: _fn,
				load: _load
			}
		}
		//缓存方法
		var _param = {
			complete: _opt.complete,
			error: _opt.error
		}
		//设置默认值
		_opt.dataType = _opt.dataType ? _opt.dataType : "json";
		_opt.timeout = _opt.timeout ? _opt.timeout : 30000;
		_opt.type = _opt.type ? _opt.type : "POST";
		_opt.load = _opt.load == undefined ? true : _opt.load;
		_opt.beforeSend = function(xhr, settings) {
			if (_opt.load) {
				//启动loading
				NProgress.start();
			}
			if ($("#hz5000-key").length > 0) {
				xhr.setRequestHeader("key", $("#hz5000-key").val());
			}
			if ($("#hz5000-token").length > 0) {
				xhr.setRequestHeader("token", $("#hz5000-token").val());
			}
			
			//判断回跳
			var _url = location.search;
			var _back = "";
			if (_url.indexOf("?") != -1) {
				var str = _url.substr(1),
				    strs = str.split("&");
				for (var i = 0; i < strs.length; i++) { 
					if ( strs[i].split("=")[0] == "back" ) {
						_back = unescape(strs[i].split("=")[1]);
						//获取hash
						_back = _back + location.hash;
						break;
					}
				} 
			}			
			xhr.setRequestHeader("ref", _back);
		}
		_opt.error = function(XMLHttpRequest, textStatus, errorThrown) {
			//判断是否为方法
//			if ($.isFunction(_param.complete)) {
//				_param.complete(XMLHttpRequest, textStatus, errorThrown);
//			}
		}
		_opt.complete = function(XMLHttpRequest, textStatus) {
			if (_opt.load) {
				//关闭loading
				NProgress.done();
			}
			//特殊状态码判断
			if (XMLHttpRequest.status == 301) {
				$.tip("重复提交表单");
			} else if (XMLHttpRequest.status == 302) {
				if (document.URL.indexOf("/home/") != -1) {
					location.href = pro_path + "/home/login.html";	
				} else {
					location.href = pro_path + "/portal/login.html";
				}
				
			}
			//判断是否为方法
			if ($.isFunction(_param.error)) {
				_param.error(XMLHttpRequest, textStatus, errorThrown);
			}
		}
		
		//数据参数格式化
		if ( $.isPlainObject(_opt.data) && !$.isEmptyObject(_opt.data) ) {
			for (var _key in _opt.data) {
				//判断类型
				if ( $.isArray(_opt.data[_key]) ) {
					var _temp = _opt.data[_key];
					//拆分数据
					for (var _i in _temp) {
						if ($.isFunction(_temp[_i])) continue;
						//判断是否存在子集
						if ($.isPlainObject(_temp[_i])) {
							for (var _j in _temp[_i]) {
								_opt.data[_key + "[" + _i + "]." + _j] = _temp[_i][_j];
							}
						} else {
							_opt.data[_key + "[" + _i + "]"] = _temp[_i];
						}
					}
					//删除key
					delete _opt.data[_key];
				}
			}
		}
		//返回ajax对象
		return $.ajax(_opt);
	},
	//提示
	tip: function(_msg) {
		if ($.messager && $.messager.show) {
			$.messager.show({
				title:'提示',
				msg: _msg,
				showType:'slide',
				style:{
					right:'',
					top:document.body.scrollTop + document.documentElement.scrollTop,
					bottom:''
				}
			});
		} else {
			alert(_msg);
		}
	},
	//提示
	alert: function(_msg, _fn, _icon) {
		//error,question,info,warning
		_icon = _icon ? _icon : 'info';
		if (arguments.length == 2) {
			if (!$.isFunction(_fn)) {
				_icon = _fn;
			}
		}
		_fn = $.isFunction(_fn) ? _fn : function(){}
		if ($.messager && $.messager.alert) {
			$.messager.alert('提示', _msg, _icon, _fn);
		} else {
			alert(_msg);
			_fn();
		}
	},
	//编码
	encode: function(_msg) {
		return _msg;
		//return encodeURIComponent( _msg );
	},
	//下载
	download: function( url ) {
        var _form = $("<form action='" + url + "' target='_blank'><input type='hidden' name='fileName' value='123123'/></form>");
        _form.appendTo('body').submit().remove();
	},
	//随机数
	random: function( num ) {
		var seed = new Date().getTime();
		seed = ( seed * 9301 + 49297 ) % 233280;
		seed = seed / ( 233280.0 );
		if (num) {
			return Math.ceil(seed * num);
		} else {
			return seed;
		}
	},
	//关键字匹配
	keyword: function(msg) {
		return /[<>;'"//\&#%]/.test(msg);
	},
	//全屏
	fullscreen: function() {
		var element = document.body;
		var requestMethod = element.requestFullScreen || //W3C
	    element.webkitRequestFullScreen ||    //Chrome等
	    element.mozRequestFullScreen || //FireFox
	    element.msRequestFullScreen; //IE11
	    if (requestMethod) {
	        requestMethod.call(element);
	    //for Internet Explorer
	    } else if (typeof window.ActiveXObject !== "undefined") {
	        var wscript = new ActiveXObject("WScript.Shell");
	        if (wscript !== null) {
	            wscript.SendKeys("{F11}");
	        }
	    }
	},
	//退出全屏
	exitfullscreen: function() {
		// 判断各种浏览器，找到正确的方法
	    var exitMethod = document.exitFullscreen || //W3C
	    document.mozCancelFullScreen ||    //Chrome等
	    document.webkitExitFullscreen || //FireFox
	    document.webkitExitFullscreen; //IE11
	    if (exitMethod) {
	        exitMethod.call(document);
	    //for Internet Explorer
	    } else if (typeof window.ActiveXObject !== "undefined") {
	        var wscript = new ActiveXObject("WScript.Shell");
	        if (wscript !== null) {
	            wscript.SendKeys("{F11}");
	        }
	    }
	}
});

$.fn.extend({
	//功能提示
	tip: function(_msg, _pos) {
		_pos == _pos ? _pos : "bottom";
		$(this).tooltip({
			position: _pos,
			content: '<span style="color:#fff;">' + _msg + '</span>',
			onShow: function(){
				$(this).tooltip('tip').css({
					backgroundColor: '#666',
					borderColor: '#666'
				});
			}
		});
	}
});